package com.bk.bands.template;

import javax.xml.bind.JAXBException;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlTransient;
import javax.xml.bind.annotation.XmlType;

import com.bk.bands.model.Field;
import com.bk.bands.model.Style;
import com.bk.bands.model.TextField;
import com.google.common.base.Strings;


@XmlRootElement(namespace = "http://com.bk.docs", name = "text")
@XmlAccessorType(XmlAccessType.PUBLIC_MEMBER)
@XmlType(propOrder = {
   "border",
   "value",
   "style",
   "ownStyle"
})
public class XmlText extends XmlField
{
   private TextField field = new TextField();

   public XmlText(XmlTemplate parentTemplate)
   {
      super(parentTemplate);
   }

   public XmlText()  { }


   @XmlElement(namespace = "http://com.bk.docs")
   public XmlBorder getBorder()
   {
      return new XmlBorder(field.getBorder());
   }

   public void setBorder(XmlBorder value)
   {
      field.setBorder(value.getBorder());
   }

   @XmlElement(namespace = "http://com.bk.docs", required = true)
   public String getValue()
   {
      return field.getValue();
   }

   public void setValue(String value)
   {
      field.setValue(value);
   }

   @XmlElement(namespace = "http://com.bk.docs", nillable = true)
   public String getStyle()
   {
      Style refStyle = field.getRefStyle();
      if(refStyle != null)
      {
         return refStyle.getName();
      }

      return null;
   }

   public void setStyle(String styleName) throws JAXBException
   {
      XmlStyle xmlStyle = getParentTemplate().findStyle(styleName);
      if(xmlStyle == null)
      {
         throw new JAXBException("Unresolved style: " + styleName);
      }
      field.setRefStyle(xmlStyle.getStyle());
   }

   @XmlElement(namespace = "http://com.bk.docs", name = "ownstyle", nillable = true)
   public XmlStyle getOwnStyle()
   {
      return new XmlStyle(field.getOwnStyle());
   }

   public void setOwnStyle(XmlStyle value)
   {
      field.setOwnStyle(value.getStyle());
   }

   @Override
   protected Field delegate()
   {
      return field;
   }
}
